package first.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
1、synchronized
将存在线程安全问题的代码放到同步代码块中
2、实现SingleThreadModel接口
servlet实现SingleThreadModel接口后，每个线程都会创建servlet实例，这样每个客户端请求就不存在共享资源的问题，但是servlet响应客户端请求的效率太低，所以已经淘汰。
3、尽可能只使用局部变量
 */

@WebServlet(name = "MyServlet5",value = "/servlet5")
public class MyServlet5 extends HttpServlet {

    private int num=0;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       // int num=0;
        num++;
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("数字count:"+num);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
